package com.coderman.common.starter.tianqing.qlexpress.listener;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.coderman.common.starter.tianqing.qlexpress.service.SpringQlExpressRunner;
import com.coderman.common.starter.tianqing.qlexpress.service.QlExpressDataHelperService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

/**
 * Description:
 * date: 2020/10/14
 *
 * @author fanchunshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class QlExpressConfigListenerImpl implements QlExpressConfigListener {


    private Logger logger = LoggerFactory.getLogger(SpringQlExpressConfigListener.class);

    @Autowired
    private QlExpressDataHelperService qlExpressDataHelperService;

    @Autowired
    private SpringQlExpressRunner qlExpressRunner;

    @Autowired
    private QlExpressConfigServiceWrapper qlExpressConfigServiceWrapper;

    @Override
    public void addListener(String[] dataIDArr, String[] groupIDArr) throws NacosException {
        Map<String,String> dataIDMap = new HashMap<>();

        if(dataIDArr != null && dataIDArr.length > 0){
            for (int i = 0;i < dataIDArr.length; i++){
                dataIDMap.put(i+"", dataIDArr[i]);
            }
        }

        if(groupIDArr == null  || groupIDArr.length == 0){
            logger.error("nacos data not fund groupIdArr ,please check your config application-nacosdatadict.yml");
            return;
        }
        ConfigService configService = qlExpressConfigServiceWrapper.getConfigService();
        for (String str : groupIDArr){
            String [] strArr = str.split("-");
            String dataID = dataIDMap.get(strArr[0]);
            String groupId = strArr[1];
            if(StringUtils.isNotEmpty(dataID)){
                String content = configService.getConfig(dataID, groupId, 3000);
                qlExpressDataHelperService.addConfig(dataID,groupId,content);
                addListener(dataID,groupId);
            }
        }
    }

    /**
     * 针对不同的dataid,groupid添加listener
     * @param dataId
     * @param groupID
     * @throws NacosException
     */
    private void addListener(final String dataId, final String groupID) throws NacosException {
        ConfigService configService = qlExpressConfigServiceWrapper.getConfigService();

        configService.addListener(dataId, groupID, new Listener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                logger.info("dataId={},groupId={} has data changed.",dataId,groupID);
                //配置数据变更需要清空编译的指令缓存
                qlExpressRunner.clearQlExpressCache();
                qlExpressDataHelperService.addConfig(dataId,groupID,configInfo);
            }

            @Override
            public Executor getExecutor() {
                return null;
            }
        });
    }
}
